<template>
  <div class="main-box">

    <el-table
      :data="memberList"
      border
      style="width: 100%"
    >
      <el-table-column
        fixed
        prop="userId"
        label="编号"
        align="center"
      />
      <el-table-column prop="userName" label="姓名" align="center" />
      <el-table-column prop="userDepartment" label="部门" width="200" align="center" />
      <el-table-column label="性别" prop="userGender" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ genderList[scope.row.userGender] }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="clazzName" label="班级" width="300" align="center" />
      <el-table-column prop="userQq" label="QQ" align="center" />
      <el-table-column prop="userTelephone" label="电话" align="center" />
      <el-table-column fixed="right" align="center" width="200">
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="输入姓名搜索" />
        </template>
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
             size="mini"
             type="danger"
             style="margin-left: 10%;"
             @click="handleDelete(scope.$index, scope.row)"
             >删除
          </el-button>
        </template>
      </el-table-column>
<!--      <el-table-column-->
<!--        align="center"-->
<!--      >-->
<!--        <template slot-scope="scope">-->
<!--          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="danger"-->
<!--            style="margin-left: 10%;"-->
<!--            @click="handleDelete(scope.$index, scope.row)"-->
<!--          >删除-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <!--编辑弹窗-->
    <div class="exit-box">
      <el-dialog title="编辑" :visible.sync="dialogEditFormVisible">
        <el-form ref="userInfoForm" :model="row">
          <el-form-item label="账号" :label-width="formLabelWidth" prop="userId">
            <el-input v-model="row.userId" autocomplete="off" placeholder="学号或工号" name="userId" disabled />
          </el-form-item>
          <el-form-item label="姓名" :label-width="formLabelWidth" prop="userName">
            <el-input v-model="row.userName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="QQ" :label-width="formLabelWidth" prop="userQq">
            <el-input v-model="row.userQq" autocomplete="off" />
          </el-form-item>
          <el-form-item label="电话" :label-width="formLabelWidth" prop="userTelephone">
            <el-input v-model="row.userTelephone" autocomplete="off" />
          </el-form-item>
          <el-form-item label="部门" :label-width="formLabelWidth" prop="userDepartment">
            <el-input v-model="row.userDepartment" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogEditFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateMemberInfo">确 定</el-button>
        </div>
      </el-dialog>
    </div>

    <!--    分页-->
    <div class="pagination-box">
      <el-pagination
        :page-size="pageSize"
        :current-page="pageNum"
        :total="total"
        :page-sizes="[5, 10, 20, 50, 200, 300, 400]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import localStorageUtil from '../../../utils/localStorageUtil'
export default {
  data() {
    return {
      memberList: [
        {
          userId: '',
          userName: '',
          userDepartment: '',
          userQq: '',
          userTelephone: '',
          userGender: '',
          clazzName: ''
        }
      ],
      /* 当前选中这一行的数据*/
      row: {},
      /* 授权弹窗控制*/
      dialogPermissionsVisible: false,
      /* 编辑弹窗控制*/
      dialogEditFormVisible: false,
      /* label 宽度*/
      formLabelWidth: '120px',
      search: '',
      genderList: ['男', '女'],
      /* 默认每页显示条数*/
      pageSize: 5,
      /* 默认显示第一页数据*/
      pageNum: 1,
      /* 用户列表总记录条数*/
      total: 1
    }
  },
  watch: {
    search(oldVal, newVal) {
      const params = {
        status: 1,
        communityId: localStorageUtil.read('SET_USER').userCommunityId,
        search: this.search
      }
      if (oldVal !== newVal && this.search !== '') {
        console.log(this.search)
        // this.$store.dispatch('user/findMemberByLikeUserName', { search: this.search, status: 1, communityId: localStorageUtil.read('SET_USER').userCommunityId }).then((res) => {
        //   this.memberList = res.data
        // })
        this.$store.dispatch('user/findMemberByLikeUserName', params).then((res) => {
          console.log(res)
          this.memberList = res.data
        })
      } else {
        this.fetchMembers()
      }
    }
  },
  created() {
    this.fetchMembers()
    this.fetchTotal()
  },
  methods: {
    /* 获取用户总记录条数*/
    fetchTotal() {
      const params = {
        status: 1,
        communityId: localStorageUtil.read('SET_USER').userCommunityId
      }
      this.$store.dispatch('user/getMemberTotal', params).then((res) => {
        this.total = res.data
      })
    },
    /* 每页显示多少条改变时间*/
    handleSizeChange(val) {
      this.pageNum = 1
      this.pageSize = val
      this.fetchUsers()
    },
    /* 页码发生改变时间*/
    handleCurrentChange(val) {
      this.pageNum = val
      this.fetchUsers()
    },
    fetchMembers() {
      const params = {
        status: 1,
        communityId: localStorageUtil.read('SET_USER').userCommunityId,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        order: 'desc'
      }
      this.$store.dispatch('user/members', params).then((res) => {
        this.memberList = res.data
      })
    },
    /* 编辑触发弹窗*/
    handleEdit(index, row) {
      this.row = row
      this.dialogEditFormVisible = true
    },
    /* 修改用户信息*/
    updateMemberInfo() {
      this.$store.dispatch('user/updateMemberInfo', this.row).then((res) => {
        if (res.code === 200) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
        }
        this.dialogEditFormVisible = false
        this.dialogPermissionsVisible = false
        this.fetchUsers()
      })
    },
    handleDelete(index, row) {
      const params = {
        communityId: localStorageUtil.read('SET_USER').userCommunityId,
        userId: row.userId
      }
      this.$confirm(`确认踢除用户：${row.userId}:${row.userName} 吗？`)
        .then(_ => {
          this.$store.dispatch('user/memberRemoveById', params).then((res) => {
            if (res.code === 200) {
              this.$message({
                showClose: true,
                message: '踢除成功',
                type: 'success'
              })
            }
            this.fetchMembers()
          })
        })
        .catch(_ => {
          this.$message({
            showClose: true,
            message: '已经取消'
          })
        })
    }
  }
}
</script>

<style>
  .main-box {
    margin: 2% 2%;
  }
  .pagination-box {
    margin: 4% 30%;
  }
</style>
